package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {

    @Autowired
    SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CachePut(cacheNames = "setmealCache",key = "#setmealDTO.id")
    public Result addSetMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.addSetMeal(setmealDTO);
        return Result.success();
    }


    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Cacheable(cacheNames = "setmealCache",key = "#setmealPageQueryDTO")
    @GetMapping("/page")
    public Result pageQuerySetMeal(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setMealService.pageQuerySetMeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetMeal(@RequestParam("ids") List<Integer> ids){
        setMealService.deleteSetMeal(ids);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Cacheable(cacheNames = "setmealCache",key = "#id")
    public Result getSetMeal(@PathVariable("id") Integer id){
        SetmealVO setMeal = setMealService.getSetMeal(id);
        return Result.success(setMeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.id")
    public Result modifySetMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.modifySetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售、停售
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",key = "#id")
    public Result modifySetMealStatus(@PathVariable("status") Integer status,@RequestParam("id")Integer id){
        setMealService.modifySetMealStatus(status,id);
        return Result.success();
    }
}
